﻿using System.Configuration;


namespace config
{
    class Cfg
    {
        private static Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        private Cfg()
        {

        }
        public static string Db_Name { get => configFile.AppSettings.Settings["db_name"].Value; set => configFile.AppSettings.Settings["db_name"].Value = value.Trim(); }
        /// <summary>
        /// 获取数据库主机名或IP
        /// </summary>
        public static string Db_Host { get => configFile.AppSettings.Settings["db_host"].Value; set => configFile.AppSettings.Settings["db_host"].Value = value.Trim(); }
        /// <summary>
        /// 获取访问数据库的用户名
        /// </summary>
        public static string Db_User { get => configFile.AppSettings.Settings["db_user"].Value; set => configFile.AppSettings.Settings["db_user"].Value = value.Trim(); }
        /// <summary>
        /// 获取访问数据库的密码
        /// </summary>
        public static string Db_Password { get => configFile.AppSettings.Settings["db_password"].Value; set => configFile.AppSettings.Settings["db_password"].Value = value.Trim(); }
        /// <summary>
        /// 获取访问数据库的端口
        /// </summary>
        public static string Db_Port { get => configFile.AppSettings.Settings["db_port"].Value; set => configFile.AppSettings.Settings["db_port"].Value = value.Trim(); }
        /// <summary>
        /// 保存内存中的配置文件内容到磁盘
        /// </summary>
        /// <returns></returns>
        public static string Save()
        {
            string result = "保存成功";
            try
            {
                configFile.Save(ConfigurationSaveMode.Full);
            }
            catch (System.Exception e)
            {
                result = e.Message;
                return result;
            }

            return result;
        }
    }
}
